#ifndef __RADAR_PROTOCOL_BLG_H__
#define __RADAR_PROTOCOL_BLG_H__

#include <stdint.h>
#include <stdbool.h>
#include "common.h"
#include "config.h"

#define RADAR_PACKET_HEADER 0x5A5A5A5A
#define RADAR_PACKET_TAIL   0xA5A5A5A5

// 定义雷达数据包结构体
typedef struct __attribute__((packed)) {
    uint32_t header;                       // 包头
    uint16_t length;                       // 数据长度
    uint8_t version;                       // 协议版本
    uint16_t type;                         // 数据类型
    uint8_t device_id[20];                 // 设备 ID
    uint8_t timestamp[8];                  // 时间戳
    uint8_t data[RADAR_PACKET_DATA_SIZE];  // 数据
    uint32_t checksum;                     // 校验和
    uint32_t tail;                         // 包尾
} radar_packet_t;

typedef struct __attribute__((packed)) {
    uint16_t obj_num;
    uint8_t lane_count;
    int32_t radar_longitude;
    int32_t radar_latitude;
    int16_t radar_height;
    uint16_t radar_north_angle;
    uint8_t radar_frame_num[3];
    int16_t radar_defection_angle;
    uint8_t radar_ip3;
    uint8_t radar_ip4;
    uint8_t reserve[4];
} data_65_header_t;

typedef struct __attribute__((packed)) {
    uint16_t obj_id;
    uint8_t obj_lane;
    uint8_t obj_type;
    uint8_t obj_length;
    uint8_t reserve1[4];
    int16_t obj_dist_y_low;
    int16_t obj_dist_x_low;
    int16_t obj_speed_y;
    int16_t obj_speed_x;
    int16_t obj_speed;
    uint8_t reserve2[6];
    uint16_t obj_yaw_angle;
    int32_t obj_longitude;
    int32_t obj_latitude;
    int16_t obj_height;
    uint8_t reserve3[4];
    uint8_t obj_id_high;
    int16_t obj_dist_y_high;
    int16_t obj_dist_x_high;
    int8_t reserve4[2];
} data_65_body_t;

typedef struct __attribute__((packed)) {
    data_65_header_t header;
    data_65_body_t body[0];
} radar_packet_65_t;

typedef struct __attribute__((packed)) {
    uint16_t flow_cycle;             // 流量周期
    uint8_t v_A_length;              // A类车长度
    uint8_t v_B_length;              // B类车长度
    uint8_t v_C_length;              // C类车长度
    uint8_t lane_count;              // 车道数
    uint8_t radar_ip_3;              // 雷达 IP 地址第三位
    uint8_t radar_ip_4;              // 雷达 IP 地址第四位
    uint16_t trigger_line_incoming;  // 进口触发线
    uint16_t trigger_line_outgoing;  // 出口触发线
    uint8_t radar_direction;         // 雷达方向
    uint8_t time_start;              // 开始时间
    uint8_t time_end;                // 结束时间
    uint8_t reserve[1];              // 保留
} data_80_header_t;

typedef struct __attribute__((packed)) {
    uint16_t way_id;                 // 道路ID
    uint16_t sum_vehicle;            // 车辆总数
    uint16_t a_flow_rate_big;        // A类车流量（大型车）
    uint16_t b_flow_rate_mid;        // B类车流量（中型车）
    uint16_t c_flow_rate_small;      // C类车流量（小型车）
    uint16_t avg_time_use_rate;      // 平均时间占有率
    uint16_t avg_flow_rate_vehicle;  // 平均车速
    uint8_t avg_length_vehicle;      // 平均车长
    uint8_t avg_dis_time;            // 平均车头时距
    uint8_t avg_dis_inter;           // 平均车身时距
    uint16_t statistic_85_Speed;     // 85%位速度
    uint16_t person_flow_rate;       // 人流量
    uint16_t flow_nonmotor;          // 非机动车流量
    uint8_t reserve[1];              // 保留
} data_80_body_t;

typedef struct __attribute__((packed)) {
    data_80_header_t header;
    data_80_body_t body[0];
} radar_packet_80_t;

typedef struct __attribute__((packed)) {
    uint16_t event_type;
    uint8_t reserve[6];
} data_84_header_t;

typedef union __attribute__((packed)) {
    struct __attribute__((packed)) vehicle_abnormality_t {
        uint16_t obj_id;
        uint8_t obj_lane;
        uint8_t obj_type;
        uint8_t obj_length;
        uint8_t obj_width;
        uint8_t reserve1[3];
        int16_t obj_dist_y_low;
        int16_t obj_dist_x_low;
        int16_t obj_speed_y;
        int16_t obj_speed_x;
        int16_t obj_speed;
        uint8_t reserve2[29];
    } va;
    struct __attribute__((packed)) queue_timeout_t {
        uint16_t test_num;
        uint16_t exceed_length;
        uint8_t reserve[44];
    } qt;
    struct __attribute__((packed)) intersection_overflow_t {
        uint16_t test_num;
        uint16_t overflow_length;
        uint8_t reserve[44];
    } io;
    struct __attribute__((packed)) traffic_jam_t {
        uint16_t test_num;
        uint16_t lane_averagev;
        int32_t traffic_jam_start;
        int32_t traffic_jam_end;
        int32_t traffic_jam_length;
        int32_t start_longitude;
        int32_t start_latitude;
        int32_t end_longitude;
        int32_t end_latitude;
        uint16_t flow_sum;
        uint16_t reserve[14];
    } tj;
    struct __attribute__((packed)) traffic_jam2_t {
        uint16_t traffic_jam_length;
        uint8_t congestion_level;
        uint8_t is_overflow;
        uint8_t lane_num;
        int16_t start_x;
        int16_t start_y;
        uint8_t reserve[39];
    } tj2;
    struct __attribute__((packed)) obstacle_t {
        uint16_t test_num;
        uint16_t obstacle_y;
        uint16_t reserve[44];
    } ob;
} data_84_body_t;

typedef struct __attribute__((packed)) {
    data_84_header_t header;
    data_84_body_t body;
} radar_packet_84_t;

typedef struct {
    uint16_t id;
    int8_t event_id;
    int8_t dev_type;
    int64_t time_stamp;
} blg_event_static_t;

void analysis_radar_buffer_blg(char *buffer, int length, uint8_t dev_type);

#endif  // !__RADAR_PROTOCOL_BLG_H__
